/**
 * 14/02/2014
 * 
 * PayRollDAO
 */
package org.cuatrovientos.erp.dao;

import java.util.List;

import org.cuatrovientos.erp.model.PayRoll;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * Implementation of PayRollDAO
 * @author inaki Torres
 * 
 */
public class PayRollDAO implements PayRollDAOInterface {

	@Autowired
	private SessionFactory sessionFactory;


	/**
	 * Constructor
	 */
	public PayRollDAO () {

	}

	/**
	 * returns Hibernate session
	 * @return
	 */
	private Session getSession () {
		return sessionFactory.getCurrentSession();
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.PayRollDAOInterface#selectById(int)
	 */
	@Transactional(readOnly = true)
	public PayRoll selectById(int id) {
		PayRoll shipment = (PayRoll) getSession().get(PayRoll.class, id);
		return shipment;
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.PayRollDAOInterface#selectAll()
	 */
	@Transactional(readOnly = true)
	public List<PayRoll> selectAll() {
		List<PayRoll> payroll = getSession().createQuery("from PayRoll").list();
		return payroll;
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.PayRollDAOInterface#selectAllCount()
	 */
	@Transactional(readOnly = true)
	public int selectAllCount() {
		List<PayRoll> payroll = getSession().createQuery("from PayRoll").list();
		return payroll.size();
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.PayRollDAOInterface#insert(org.cuatrovientos.erp.model.PayRoll)
	 */
	@Transactional
	public void insert(PayRoll payroll) {
		Integer id = (Integer) getSession().save(payroll);
		payroll.setId(id);           
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.PayRollDAOInterface#update(org.cuatrovientos.erp.model.PayRoll)
	 */
	@Transactional
	public void update(PayRoll payroll) {
		getSession().merge(payroll);
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.PayRollDAOInterface#delete(org.cuatrovientos.erp.model.PayRoll)
	 */
	@Transactional
	public void delete(PayRoll payroll) {
		getSession().delete(payroll);
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.PayRollDAOInterface#saveOrUpdate(org.cuatrovientos.erp.model.PayRoll)
	 */
	@Transactional
	public void saveOrUpdate(PayRoll payroll) {
		getSession().saveOrUpdate(payroll);
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.PayRollDAOInterface#deleteById(int)
	 */
	@Transactional
	public void deleteById(int id) {
		PayRoll payroll = (PayRoll) getSession().load(PayRoll.class,id);
		getSession().delete(payroll);
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.PayRollDAOInterface#search(java.lang.String)
	 */
	@Transactional(readOnly = true)
	public List<PayRoll> search(String name) {
		Query query = 
				getSession().
				createQuery("from PayRoll s where s.name like :name");
		
		query.setParameter("name", "%" + name + "%");

		List<PayRoll> payroll = query.list();

		return payroll;
	}

}
